package com.common.valid;



import com.common.Constants;
import com.common.valid.annotation.Phone;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PhoneValidator implements ConstraintValidator<Phone,String> {
    private static final Pattern PHONE_PATTERN = Pattern.compile(Constants.VALID_PHONE_PATTERN);
    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if(!Objects.isNull(value)){
            System.err.println(value);
            Matcher m = PHONE_PATTERN.matcher(value);
            return m.matches();
        }
        return false;
    }
}
